// Copyright (C) 2024 Kumo inc.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//
//

#include <kllm/openai/embedding.h>
#include <turbo/log/logging.h>
#include <kllm/utility/embedding.h>

namespace kllm {

    void format_aoi_embedding_response(const KaiResponse &response, nlohmann::ordered_json &obj, std::string &errmsg,
                                        int &code) {
        CHECK(response.query_type() == QUERY_EMBEDDING)
                << "bad query type for this function, it should be QUERY_EMBEDDING";
        if (response.status().has_errmsg()) {
            errmsg = response.status().errmsg();
        }
        code = response.status().code();

        nlohmann::ordered_json embedding_json;
        embedding_json["object"] = "list";
        embedding_json["data"] = nlohmann::ordered_json::array();
        std::vector<float> embd_res;
        for (auto &it: response.embedding_response().embedding()) {
            nlohmann::ordered_json json;
            embd_res.assign(it.embedding().begin(), it.embedding().end());
            json["embedding"] = embd_res;
            json["index"] = it.index();
            json["object"] = "embedding";
            embedding_json["data"].push_back(std::move(json));
        }
        embedding_json["usage"] = nlohmann::ordered_json{ // TODO: fill
                {"prompt_tokens", 0},
                {"total_tokens",  0}
        };
        obj = std::move(embedding_json);
    }

}
